<?php
class galleryEditAction extends sfActions
{
	public function execute($request)
	{
		$this->gallery_id = $request->getParameter('gallery_id');
		if ($this->gallery_id) {
			$this->gallery = Doctrine_Core::getTable('VTNSgallery')->find($this->gallery_id);
			if ($this->gallery)
				$this->gallery_form = new AdminGalleryForm($this->gallery);
		}
        else {
            $this->gallery_form = new AdminGalleryForm();
        }
		if ($request->hasParameter('gallery')) {
			$form = $request->getParameter('gallery');
			
			$this->status = $this->updateGalleryFromRequest($this->gallery, $form);
			try {
				$files = $this->getRequest()->getFiles();
                $file = $files['gallery']['picture'];
                // delete old file if exist
                if($file['size'] > 0) {
                    if(isset($this->gallery->picture) && $this->gallery->picture != null) {
                        foreach($this->gallery->dimensions as $dimension) {
                            $path_file = "uploads/gallery/".$dimension['name'].$this->gallery->picture;
                            if(file_exists($path_file))
                                unlink($path_file);
                       }
                    $path_file = "uploads/gallery/".$this->gallery->picture;
                    if(file_exists($path_file))
                       unlink($path_file);
                   }
                // upload new file

					$tmpName = $file['tmp_name'];
					$fileName = $file['name'];
					$fileExt = trim(strrchr($fileName,'.'));
                    
                    $fileName = $this->gallery->gallery_id.$fileExt;
					$img = new sfImage($tmpName, $file['type']);
					$img->saveAs(sfConfig::get('sf_upload_dir').'/gallery/'.$fileName, $file['type']);
					
					foreach($this->gallery->dimensions as $dimensions) {
                        $fileName1 = $dimensions['name'].($this->gallery->gallery_id).$fileExt;                    
					    $img = new sfImage($tmpName, $file['type']);
                        if($dimensions['width'] != 0 && $dimensions['height'] != 0 )
                            $img->thumbnail($dimensions['width'],$dimensions['height'],'center');
                        $img->setQuality(100);
					    $img->saveAs(sfConfig::get('sf_upload_dir').'/gallery/'.$fileName1, $file['type']);                            
                        }
					
					$this->gallery->picture = $fileName;
					$this->gallery->save();
					$this->upload_success = true;		
				}
			} catch (sfException $e) {
				//echo $e->getMessage();
			}
			
			if ($this->status) {
				$this->gallery->save();
				$this->redirect('gallery/galleryList?gallery_category_id='.$this->gallery->gallery_category_id.'&notify=editSuccess');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateGalleryFromRequest(&$gallery, $form)
	{
		if ($form['content'])
			$gallery->content = $form['content'];
		if ($form['name'])
			$gallery->name = $form['name'];	
		if (isset($form['is_active']))
			$gallery->is_active = 1;
		else
			$gallery->is_active = 0;
		if ($form['description'])
			$gallery->description = $form['description'];

		$gallery->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>